package com.ist.smartstation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ist.smartstation.dao.db.sqlite.PassengerSqliteDao;
import com.ist.smartstation.entity.Passenger;

public class RegisterActivity extends Activity {
	
//	private static final String LOG_TAG = "RegisterActivity";
	
	private EditText nameField;
	private NumberPicker numberField;
//	private AutoCompleteTextView destinationField;
	private Button submitButton;
	private Button mapButton;
	
	private PassengerSqliteDao passengerDao;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registerform);
		initFields();
		passengerDao = new PassengerSqliteDao(this);
        mapButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
            	createMap();
            }
        });
		submitButton.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				savePassenger();
			}

		});
	}
	
    private void createMap(){
    	Intent i = new Intent(this, Map.class);
    	startActivity(i);
    }
	
	private void initFields() {
		nameField = (EditText) findViewById(R.id.namefield);
		numberField = (NumberPicker) findViewById(R.id.nopassengersfield);
//		destinationField = (AutoCompleteTextView) findViewById(R.id.destinationfield);
		submitButton = (Button) findViewById(R.id.submitbutton);
		mapButton = (Button) findViewById(R.id.mapbutton);
	}
	
//	private void collectDataAndReturn() {
//		Intent i = new Intent();
//		Bundle extras = new Bundle();
//		extras.putSerializable("passenger", savePassenger());
//		i.putExtras(extras);
//		setResult(RESULT_OK, i);
//		finish();
//	}
	
    private void savePassenger() {
		passengerDao.open();
        Passenger passenger = new Passenger();
        passenger.mName = nameField.getText().toString();
        passenger.mPartyNumber = numberField.getValue();
//        passenger.mDestination = destinationField.getText().toString();
        passenger = passengerDao.createPassenger(passenger);
        passengerDao.close();
    }
}
